{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### AlexNet\n",
    "    AlexNet网络诞生于2012年，当年ImageNet竞赛的冠军，Top5错误率为16.4%\n",
    "    Alex Krizhevsky, Ilya Sutskever, Geoffrey E. Hinton. ImageNet Classification with Deep Convolutional NeuralNetworks. In NIPS, 2012.\n",
    "    \n",
    "    结构：五层卷积，三层全连接\n",
    "    输入：32*32*3\n",
    "    C（核：96*3*3， 步长：1，填充：valid ）\n",
    "    B（Yes，LRN*）\n",
    "    A（relu）\n",
    "    P（max，核：3*3，步长：2 ）\n",
    "    D（None）\n",
    "    \n",
    "    C（核：256*3*3，步长：1，填充：valid ）\n",
    "    B（Yes，LRN*）\n",
    "    A（relu）\n",
    "    P（max，核：3*3，步长：2 ）\n",
    "    D（None）\n",
    "    \n",
    "    C（核：384*3*3，步长：1，填充：same ）\n",
    "    B（None）\n",
    "    A（relu）\n",
    "    P（None）\n",
    "    D（None）\n",
    "    \n",
    "    C（核：384*3*3，步长：1，填充：same ）\n",
    "    B（None）\n",
    "    A（relu）\n",
    "    P（None）\n",
    "    D（None）\n",
    "    \n",
    "    C（核：256*3*3，步长：1，填充：same ）\n",
    "    B（None）\n",
    "    A（relu）\n",
    "    P（ max，核：3*3，步长：2 ）\n",
    "    D（None）\n",
    "    \n",
    "    Dense（神经元：2048，激活：relu，Dropout：0.5）\n",
    "    Dense（神经元：2048，激活：relu，Dropout：0.5）\n",
    "    Dense（神经元：10，激活：softmax）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 50000 samples, validate on 10000 samples\n",
      "Epoch 1/5\n",
      "50000/50000 [==============================] - 1678s 34ms/sample - loss: 1.6251 - sparse_categorical_accuracy: 0.4033 - val_loss: 1.6622 - val_sparse_categorical_accuracy: 0.4142\n",
      "Epoch 2/5\n",
      "50000/50000 [==============================] - 1633s 33ms/sample - loss: 1.2898 - sparse_categorical_accuracy: 0.5446 - val_loss: 1.2542 - val_sparse_categorical_accuracy: 0.5636\n",
      "Epoch 3/5\n",
      "50000/50000 [==============================] - 1549s 31ms/sample - loss: 1.1480 - sparse_categorical_accuracy: 0.5991 - val_loss: 1.3304 - val_sparse_categorical_accuracy: 0.5310\n",
      "Epoch 4/5\n",
      "50000/50000 [==============================] - 1521s 30ms/sample - loss: 1.0718 - sparse_categorical_accuracy: 0.6285 - val_loss: 1.4420 - val_sparse_categorical_accuracy: 0.5262\n",
      "Epoch 5/5\n",
      "35456/50000 [====================>.........] - ETA: 6:58 - loss: 1.0089 - sparse_categorical_accuracy: 0.6513"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "import os\n",
    "import numpy as np\n",
    "from matplotlib import pyplot as plt\n",
    "from tensorflow.keras.layers import Conv2D, BatchNormalization, Activation, MaxPool2D, Dropout, Flatten, Dense\n",
    "from tensorflow.keras import Model\n",
    "\n",
    "np.set_printoptions(threshold=np.inf)\n",
    "\n",
    "cifar10 = tf.keras.datasets.cifar10\n",
    "(x_train, y_train), (x_test, y_test) = cifar10.load_data()\n",
    "x_train, x_test = x_train / 255.0, x_test / 255.0\n",
    "\n",
    "\n",
    "class AlexNet8(Model):\n",
    "    def __init__(self):\n",
    "        super(AlexNet8, self).__init__()\n",
    "###########################第一层################################\n",
    "        self.c1 = Conv2D(filters=96, kernel_size=(3, 3))\n",
    "        self.b1 = BatchNormalization()\n",
    "        self.a1 = Activation('relu')\n",
    "        self.p1 = MaxPool2D(pool_size=(3, 3), strides=2)\n",
    "###########################第二层################################\n",
    "        self.c2 = Conv2D(filters=256, kernel_size=(3, 3))\n",
    "        self.b2 = BatchNormalization()\n",
    "        self.a2 = Activation('relu')\n",
    "        self.p2 = MaxPool2D(pool_size=(3, 3), strides=2)\n",
    "###########################第三层################################\n",
    "        self.c3 = Conv2D(filters=384, kernel_size=(3, 3), padding='same',\n",
    "                         activation='relu')\n",
    "###########################第四层################################                       \n",
    "        self.c4 = Conv2D(filters=384, kernel_size=(3, 3), padding='same',\n",
    "                         activation='relu')\n",
    "###########################第五层################################                       \n",
    "        self.c5 = Conv2D(filters=256, kernel_size=(3, 3), padding='same',\n",
    "                         activation='relu')\n",
    "        self.p3 = MaxPool2D(pool_size=(3, 3), strides=2)\n",
    "\n",
    "        self.flatten = Flatten()\n",
    "###########################第六层################################\n",
    "        self.f1 = Dense(2048, activation='relu')\n",
    "        self.d1 = Dropout(0.5)\n",
    "###########################第七层################################\n",
    "        self.f2 = Dense(2048, activation='relu')\n",
    "        self.d2 = Dropout(0.5)\n",
    "###########################第八层################################\n",
    "        self.f3 = Dense(10, activation='softmax')\n",
    "\n",
    "    def call(self, x):\n",
    "        x = self.c1(x)\n",
    "        x = self.b1(x)\n",
    "        x = self.a1(x)\n",
    "        x = self.p1(x)\n",
    "\n",
    "        x = self.c2(x)\n",
    "        x = self.b2(x)\n",
    "        x = self.a2(x)\n",
    "        x = self.p2(x)\n",
    "\n",
    "        x = self.c3(x)\n",
    "\n",
    "        x = self.c4(x)\n",
    "\n",
    "        x = self.c5(x)\n",
    "        x = self.p3(x)\n",
    "\n",
    "        x = self.flatten(x)\n",
    "        x = self.f1(x)\n",
    "        x = self.d1(x)\n",
    "        x = self.f2(x)\n",
    "        x = self.d2(x)\n",
    "        y = self.f3(x)\n",
    "        return y\n",
    "\n",
    "\n",
    "model = AlexNet8()\n",
    "\n",
    "model.compile(optimizer='adam',\n",
    "              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),\n",
    "              metrics=['sparse_categorical_accuracy'])\n",
    "\n",
    "checkpoint_save_path = \"./checkpoint/AlexNet8.ckpt\"\n",
    "if os.path.exists(checkpoint_save_path + '.index'):\n",
    "    print('-------------load the model-----------------')\n",
    "    model.load_weights(checkpoint_save_path)\n",
    "\n",
    "cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_save_path,\n",
    "                                                 save_weights_only=True,\n",
    "                                                 save_best_only=True)\n",
    "\n",
    "history = model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1,\n",
    "                    callbacks=[cp_callback])\n",
    "model.summary()\n",
    "\n",
    "# print(model.trainable_variables)\n",
    "file = open('./weights.txt', 'w')\n",
    "for v in model.trainable_variables:\n",
    "    file.write(str(v.name) + '\\n')\n",
    "    file.write(str(v.shape) + '\\n')\n",
    "    file.write(str(v.numpy()) + '\\n')\n",
    "file.close()\n",
    "\n",
    "###############################################    show   ###############################################\n",
    "\n",
    "# 显示训练集和验证集的acc和loss曲线\n",
    "acc = history.history['sparse_categorical_accuracy']\n",
    "val_acc = history.history['val_sparse_categorical_accuracy']\n",
    "loss = history.history['loss']\n",
    "val_loss = history.history['val_loss']\n",
    "\n",
    "plt.subplot(1, 2, 1)\n",
    "plt.plot(acc, label='Training Accuracy')\n",
    "plt.plot(val_acc, label='Validation Accuracy')\n",
    "plt.title('Training and Validation Accuracy')\n",
    "plt.legend()\n",
    "\n",
    "plt.subplot(1, 2, 2)\n",
    "plt.plot(loss, label='Training Loss')\n",
    "plt.plot(val_loss, label='Validation Loss')\n",
    "plt.title('Training and Validation Loss')\n",
    "plt.legend()\n",
    "plt.show()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
